package com.cn.kabalifeuser.ui.my.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.ui.integral.bean.DefaultAddressBean;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.my.adapter.AddressCheckAdapter;
import com.cn.kabalifeuser.ui.my.bean.AddressListBean;
import com.cn.kabalifeuser.ui.my.bean.AddressProvinceListBean;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.view.PopupWindow.CommonPopupWindow;
import com.cn.kabalifeuser.view.PopupWindow.CommonUtil;

import org.w3c.dom.Text;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/9/3.
 * function:
 */
public class IAddressAddActivity extends IBaseActivity<IAddressView, IAddressPresenter> implements IAddressView, CommonPopupWindow.ViewInterface {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_add)
    TextView etAdd;
    @BindView(R.id.et_address)
    EditText etAddress;


    private CommonPopupWindow popupWindowAddress;


    List<AddressProvinceListBean.ResultBean> mListCitys;


    String province,
            city,
            area,
            street;
    int address1 = 0;
    int address2 = 0;
    int address3 = 0;
    int address4 = 0;
    int signAddress = 1;
    //创建适配器
    AddressCheckAdapter addressCheckAdapter;
    Bundle bundle;
    int sign;
    AddressListBean.ResultBean.DataBean dataBean;
    boolean isIsgn = true;

    @Override
    protected IAddressPresenter createPresenter() {
        return new IAddressPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_address;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        bundle = getIntent().getExtras();
        sign = bundle.getInt("sign");
        if (sign == -1) {
            setTitleBar("新增地址");
        } else {
            setTitleBar("修改地址");
            dataBean = (AddressListBean.ResultBean.DataBean) bundle.getSerializable("addressBean");
            if (dataBean != null) {
                etName.setText(dataBean.getName());
                etPhone.setText(dataBean.getTrue_phone());
                etAdd.setText(dataBean.getProvince() + dataBean.getCity() + dataBean.getArea() + dataBean.getStreet());
                province = dataBean.getProvince();
                city = dataBean.getCity();
                area = dataBean.getArea();
                street = dataBean.getStreet();
                etAddress.setText(dataBean.getInfo());
                address1 = dataBean.getProvince_id();
                address2 = dataBean.getCity_id();
                address3 = dataBean.getArea_id();
                address4 = dataBean.getStreet_id();
                signAddress = 4;
                isIsgn = true;
            }

        }
    }

    @OnClick({R.id.et_add, R.id.tv_hi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.et_add:
                //省
                hideKeyboard(etPhone);
                isIsgn = true;
                if (signAddress == 4) {
                    signAddress = 4;
                    mPresenter.onCityListData(address3);
                } else {
                    signAddress = 1;
                    mPresenter.onProvinceListData();
                }

                break;
            case R.id.tv_hi:
                hideKeyboard(etPhone);
                break;
        }
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return getMe();
    }

    @Override
    public void onAddressAddSuccess(MsgBean bean) {
        RxToast.success(bean.getMsg());
        finish();
    }

    @Override
    public void onAddressSuccess(DefaultAddressBean bean) {

    }

    @Override
    public void onAddressListSuccess(AddressListBean bean) {

    }

    @Override
    public void onProvinceListSuccess(AddressProvinceListBean bean) {
        if (bean.getResult() != null && bean.getResult().size() > 0) {
            mListCitys = bean.getResult();
            showCoupon();
        }
    }

    @Override
    public void onCityListSuccess(AddressProvinceListBean bean) {
        if (bean.getResult() != null && bean.getResult().size() > 0) {
            mListCitys = bean.getResult();
            if (!isIsgn)
                addressCheckAdapter.replaceData(mListCitys);
            else showCoupon();
        }
    }


    /**
     * 优惠劵
     */
    private void showCoupon() {
        if (popupWindowAddress != null && popupWindowAddress.isShowing()) return;
        View upView = LayoutInflater.from(getMe()).inflate(R.layout.pw_address, null);
        //测量View的宽高
        CommonUtil.measureWidthAndHeight(upView);
        popupWindowAddress = new CommonPopupWindow.Builder(getMe())
                .setView(R.layout.pw_address)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, upView.getMeasuredHeight())
                .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                .setAnimationStyle(R.style.AnimUp)
                .setViewOnclickListener(this)
                .create();
        popupWindowAddress.showAtLocation(findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void getChildView(View view, int layoutResId) {
        switch (layoutResId) {
            //todo
            case R.layout.pw_address:
                RecyclerView rvCoupon = view.findViewById(R.id.recyclerView);
                rvCoupon.setLayoutManager(new LinearLayoutManager(getMe()));
                TextView tv_1 = view.findViewById(R.id.tv_1);
                TextView tv_2 = view.findViewById(R.id.tv_2);
                TextView tv_3 = view.findViewById(R.id.tv_3);
                TextView tv_4 = view.findViewById(R.id.tv_4);

                if (!TextUtils.isEmpty(province)) {
                    tv_1.setText(province);
                    tv_1.setVisibility(View.VISIBLE);
                }
                if (!TextUtils.isEmpty(city)) {
                    tv_2.setVisibility(View.VISIBLE);
                    tv_2.setText(city);
                }

                if (!TextUtils.isEmpty(area)) {
                    tv_3.setVisibility(View.VISIBLE);
                    tv_3.setText(area);
                }

                if (!TextUtils.isEmpty(street)) {
                    tv_4.setVisibility(View.VISIBLE);
                    tv_4.setText(street);
                }

                tv_1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(tv_2.getText().toString().toString())) {
                            tv_1.setText("");
                            tv_1.setVisibility(View.GONE);
                            signAddress = 1;
                            address1 = 0;
                            province = "";
                            etAdd.setText("");
                            isIsgn = false;
                            mPresenter.onCityListData(1);
                        }

                    }
                });
                tv_2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(tv_3.getText().toString().toString())) {
                            tv_2.setText("");
                            tv_2.setVisibility(View.GONE);
                            address2 = 0;
                            signAddress = 2;
                            city = "";
                            isIsgn = false;
                            mPresenter.onCityListData(address1);
                        }
                    }
                });
                tv_3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (TextUtils.isEmpty(tv_4.getText().toString().toString())) {
                            tv_3.setText("");
                            tv_3.setVisibility(View.GONE);
                            signAddress = 3;
                            address3 = 0;
                            area = "";
                            isIsgn = false;
                            mPresenter.onCityListData(address2);
                        }
                    }
                });
                tv_4.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tv_4.setText("");
                        tv_4.setVisibility(View.GONE);
                        signAddress = 4;
                        address4 = 0;
                        street = "";
                        isIsgn = false;
                        mPresenter.onCityListData(address3);
                    }
                });
                //创建适配器
                addressCheckAdapter = new AddressCheckAdapter(R.layout.pw_address_item, mListCitys);
                //给RecyclerView设置适配器
                rvCoupon.setAdapter(addressCheckAdapter);
                addressCheckAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        if (signAddress == 1) {
                            tv_1.setVisibility(View.VISIBLE);
                            tv_1.setText(addressCheckAdapter.getItem(position).getName());
                            address1 = addressCheckAdapter.getItem(position).getId();
                            signAddress = 2;
                            province = tv_1.getText().toString().trim();
                            isIsgn = false;
                            mPresenter.onCityListData(addressCheckAdapter.getItem(position).getId());
                        } else if (signAddress == 2) {
                            tv_2.setVisibility(View.VISIBLE);
                            tv_2.setText(addressCheckAdapter.getItem(position).getName());
                            address2 = addressCheckAdapter.getItem(position).getId();
                            city = tv_2.getText().toString().trim();
                            signAddress = 3;
                            mPresenter.onCityListData(addressCheckAdapter.getItem(position).getId());
                        } else if (signAddress == 3) {
                            tv_3.setVisibility(View.VISIBLE);
                            tv_3.setText(addressCheckAdapter.getItem(position).getName());
                            address3 = addressCheckAdapter.getItem(position).getId();
                            area = tv_3.getText().toString().trim();
                            etAdd.setText(province + city + area);
                            signAddress = 4;
                            isIsgn = false;
                            mPresenter.onCityListData(addressCheckAdapter.getItem(position).getId());
                        } else {
                            tv_4.setVisibility(View.VISIBLE);
                            tv_4.setText(addressCheckAdapter.getItem(position).getName());
                            address4 = addressCheckAdapter.getItem(position).getId();
                            street = tv_4.getText().toString().trim();
                            etAdd.setText(province + city + area + street);
                            popupWindowAddress.dismiss();
                        }

                    }
                });

                view.findViewById(R.id.tv_btn_colce).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindowAddress.dismiss();
                    }
                });
                break;
        }
    }


    @OnClick(R.id.tv_btn_next)
    public void onViewClicked() {
        if (!TextUtils.isEmpty(etName.getText().toString().trim())) {
            if (!TextUtils.isEmpty(etPhone.getText().toString().trim())) {
                if (!TextUtils.isEmpty(etAdd.getText().toString().trim())) {
                    LoggerUtils.e("查看地址:省：" + address1 +"市："+ address2 +"区："+ address3 +"接到："+ address4);
                    LoggerUtils.e("查看地址:省：" + province +"市："+ city +"区："+ area +"接到："+ street);
                    if (sign == -1) {
                        mPresenter.onAddressAddData(
                                etName.getText().toString().trim(),
                                etPhone.getText().toString().trim(),
                                address1, address2, address3, address4,
                                etAddress.getText().toString().trim(),
                                "add", 0
                        );
                    } else {
                        mPresenter.onAddressAddData(
                                etName.getText().toString().trim(),
                                etPhone.getText().toString().trim(),
                                address1, address2, address3, address4,
                                etAddress.getText().toString().trim(),
                                "up", dataBean.getId()
                        );
                    }
                } else
                    RxToast.error("请选择收货地址");
            } else
                RxToast.error("请输入联系方式");
        } else
            RxToast.error("请输入收货人");
    }
}
